package com.service.lx;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.lx.ContentDAO;
import com.dao.lx.DiystatusDAO;
import com.entity.Content;
import com.entity.Dept;
import com.entity.Diystatus;
import com.service.ServiceException;


@Service
@Transactional(rollbackFor = Exception.class)
public class ContentService {
	@Autowired
	private ContentDAO contentDAO;
	@Autowired
	private DiystatusDAO diystatusDAO;
	/**
	 * 添加/修改/逻辑删除
	 * @param content
	 * @throws ServiceException
	 */
	public void addAndUpdateContent(Content content) throws ServiceException{
		try {
			if(content.getDiystatusId()!=null){
				Diystatus diystatus=diystatusDAO.findOne(content.getDiystatusId());
				if(diystatus!=null){
					content.setDiystatus(diystatus);
				}else{
					content.setDiystatus(null);
				}
			}
			Content content2=contentDAO.findOne(content.getContentId());
			if(content2==null){
				content.setContentIfdelete(new BigDecimal(1));
			}
			content.setContentIntodate(new Date());
			contentDAO.save(content);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param contentId
	 * @throws ServiceException
	 */
	public void deleteContent(Long contentId) throws ServiceException{
		try {
			contentDAO.delete(contentId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	

	/**
	 * 逻辑删除
	 * @param contentId
	 */
	public void UpdateUserIfdelete(Long contentId) {
		Content content = contentDAO.findOne(contentId);
		content.setContentIfdelete(new BigDecimal(0));
		addAndUpdateContent(content);
	}
	
	/**
	 * 批量删除
	 * @param contentIds
	 */
	public void deletArrIfDelete(List<Long> contentIds) {
		if(contentIds != null) {
			for(Long contentId: contentIds) {
				Content content = contentDAO.findOne(contentId);
				content.setContentIfdelete(new BigDecimal(0));
				addAndUpdateContent(content);
			}
		}
	}
	
	/**
	 * 单条查询
	 * @param contentId
	 * @return
	 */
	public Content findOne(Long contentId) {
		return contentDAO.findOne(contentId);
	}
	

	/**
	 * 单条查询
	 * @param contentId
	 * @return
	 */
	public List<Content> findall(Diystatus diystatus) {
		Diystatus diystatus2=diystatusDAO.findOne(diystatus.getDiystatusId());
		return contentDAO.findAllContentIfdelete(diystatus2);
	}

}
